package com.hexb.smh.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.hexb.core.utils.ObjectUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author : hexb
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Commission {

    private Integer id;

    @ApiModelProperty("折扣最低(不包含)")
    private BigDecimal minDiscount;

    @ApiModelProperty("折扣最高(包含)")
    private BigDecimal maxDiscount;

    @ApiModelProperty("成交金额最低")
    private BigDecimal minAmount;

    @ApiModelProperty("成交金额最高")
    private BigDecimal maxAmount;

    @ApiModelProperty("提成金额")
    private BigDecimal amount;

    @ApiModelProperty("折扣key,用于标识相同的折扣范围")
    private String discountKey;

    @ApiModelProperty("订单成交价key,用于标识相同的成交价范围")
    private String amountKey;

    @ApiModelProperty("品牌Id")
    private Integer brandId;

    @ApiModelProperty("创建时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date createTime;

    @ApiModelProperty("最后一次修改时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date updateTime;

    @ApiModelProperty("创建人")
    private Integer createBy;

    @ApiModelProperty("最后一次修改人")
    private Integer updateBy;

    public String createDiscountKey() {
        return (minDiscount == null ? "" : minDiscount.toString()) + ":" + (maxDiscount == null ? "" : maxDiscount.toString());
    }

    public String createAmountKey() {
        return (minAmount == null ? "" : minAmount.toString()) + ":" + (maxAmount == null ? "" : maxAmount.toString());
    }

    public Commission(String minDiscount, String maxDiscount, String minAmount, String maxAmount, String amount) {
        this.minDiscount = ObjectUtils.safeCall(() -> new BigDecimal(minDiscount), null);
        this.maxDiscount = ObjectUtils.safeCall(() -> new BigDecimal(maxDiscount), null);
        this.minAmount = ObjectUtils.safeCall(() -> new BigDecimal(minAmount), null);
        this.maxAmount = ObjectUtils.safeCall(() -> new BigDecimal(maxAmount), null);
        this.amount = ObjectUtils.safeCall(() -> new BigDecimal(amount), null);
    }

    public String getAmountString() {
        return amount == null ? null : amount.toString();
    }
}
